import {exec} from 'child_process';
import ConfigTool from '../../com/configTool';
import path from 'path';

const getMemoryUsage = async () => {
    // const memoryUsage = process.memoryUsage();
    //
    // const heapTotalFormat = Math.floor(memoryUsage.heapTotal / 1000 / 1000 ) + 'GB';
    // const heapUsedFormat = Math.floor(memoryUsage.heapUsed / 1000 / 1000 ) + 'GB';
    // const externalFormat = Math.floor(memoryUsage.external / 1000 / 1000 * 10) + 'GB';
    //
    // console.log( process.memoryUsage());
    // return {
    //     ...memoryUsage,
    //     heapTotalFormat,
    //     heapUsedFormat,
    //     externalFormat
    // };

    const scriptPath = path.join(ConfigTool.wwwPath, '/dll/logMemoryUsage.js');
    const command = 'node ' + scriptPath;

    return new Promise((resolve, reject) => {
        exec(command, {maxBuffer: 1024 * 1000 * 10}, (error: any, stdout: string, stderr: string) => {
            if (error) {
                console.error(`getDeviceList 错误: ${error.message}`);
                return null;
            }
            if (stderr) {
                console.error(`stderr: ${stderr}`);
                return null;
            }
            resolve(JSON.parse(stdout));
        });
    });
};

export default getMemoryUsage;